(function() {
    appCommon.controller('indexCtr', indexCtroller);

    function indexCtroller($rootScope, $scope, $http, $httpParamSerializer) {
        $rootScope.view = "";
        $scope.items = [];
        $scope.noMore = "加载中...";
        var nowPage = 1;
        var count = 4;        
        var search = location.href.split("index?type=");
        var type = search.length > 1 ? search[1] : "";
        var busy = false;

        //点赞
        $scope.good = function(item) {
            $http({
                method: 'POST',
                url: '/index.php?m=home&a=good',
                data: $httpParamSerializer({
                    "post_id": item.post_id,
                }),
                headers: {
                    'Content-Type': 'application/x-www-form-urlencoded'
                }
            }).then(function successCallback(response) {
                //未登录
                if (response.data.type == null) {
                    window.location.href = "/#/login";
                } else {
                    item.isGood = response.data.type;
                    item.goodCount = response.data.goodCount;
                }
            }, function errorCallback(response) {
                console.log(response)
            });
        }

        $scope.loadMore = function() {
            //防止重复加载
            if (busy) return;
            busy = true;

            $http({
                method: 'POST',
                url: '/index.php?m=home&a=getArticleList',
                data: $httpParamSerializer({
                    "type": type,
                    'nowPage': nowPage,
                    "count": count,
                }),
                headers: {
                    'Content-Type': 'application/x-www-form-urlencoded'
                }
            }).then(function successCallback(response) {
                nowPage++;
                var items = response.data.articleList;
                for (var i = 0; i < items.length; i++) {
                    $scope.items.push(items[i]);
                }

                if(items.length > 3) {                   
                    busy = false;
                }else {
                    $scope.noMore = "没有更多...";
                    busy = true;
                }

            }, function errorCallback(response) {
                console.log(response)
            });
        };
    }
})();